package com.heima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.domain.dto.CheckSchemaDTO;
import com.heima.domain.pojo.CheckSchema;
import com.heima.response.Result;
import com.heima.service.CheckSchemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/checkShema")
public class CheckSchemaController {

    @Autowired
    private CheckSchemaService checkSchemaService;

    //分页查询
    @PostMapping
    public Result<IPage<CheckSchema>> selectAllandquery(@RequestBody CheckSchemaDTO dto){
        return Result.resp(checkSchemaService.selectAllandquery(dto));
    }

    @PostMapping("/add")
    public Result add(@RequestBody CheckSchema checkSchema){
        checkSchemaService.add(checkSchema);
        return Result.ok();
    }

    //回显
    @GetMapping("/{id}")
    public Result getId(@PathVariable Integer id){
        return Result.ok(checkSchemaService.getId(id));
    }

    //修改
    @PutMapping
    public Result update(@RequestBody CheckSchema checkSchema){
        checkSchemaService.update(checkSchema);
        return Result.ok();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids ) {
        // 将字符串ID分割并直接转换为List
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(String::trim)
                .map(Integer::valueOf)
                .collect(Collectors.toList());

        // 使用MyBatis-Plus的批量删除功能
        checkSchemaService.deleteByIds(idList);
        return Result.ok();

    }
    @GetMapping("/get_list")
    public Result getList(){
        List<CheckSchema> list  = checkSchemaService.list();
        return Result.ok(list);
    }

}
